package com.project.erp.service.crm.customer.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.project.erp.common.utils.Page;
import com.project.erp.dao.crm.customer.CrmCustomerDao;
import com.project.erp.model.crm.customer.CrmCustomer;
import com.project.erp.service.crm.customer.CrmCustomerService;

@Service
@Transactional
public class CrmCustomerServiceImpl implements CrmCustomerService {

	@Autowired
	private CrmCustomerDao crmCustomerDao;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Page<CrmCustomer> getCrmCustomerPageBySearch(Page<CrmCustomer> page,
			CrmCustomer crmCustomer) {
		return crmCustomerDao.getCrmCustomerPageBySearch(page, crmCustomer);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void saveCrmCustomer(CrmCustomer crmCustomer) {
		crmCustomer.setCreateTime(new Date());
		crmCustomerDao.save(crmCustomer);
	}

	//只读事务，可以提高效率
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public CrmCustomer getCrmCustomerById(Long crmCustomerId) {
		return crmCustomerDao.get(crmCustomerId);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void editCrmCustomer(CrmCustomer crmCustomer) {
		crmCustomerDao.save(crmCustomer);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void delCrmCustomer(String[] crmCustomerIds) {
		if(null == crmCustomerIds || crmCustomerIds.length == 0){
			return;
		}
		List<Long> ids = new ArrayList<Long>();
		for (String crmCustomerId : crmCustomerIds) {
			ids.add(Long.valueOf(crmCustomerId));
		}
		//删除主表信息
		crmCustomerDao.deleteByIdsList(ids);
	}

	@Override
	public Map<String, Map<String, String>> getAllCrmCustomers() {
		return null;
	}

	//只读事务，可以提高效率
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public CrmCustomer getCrmCustomerByCusNameEn(String cusNameEn) {
		return crmCustomerDao.getCrmCustomerByCusNameEn(cusNameEn);
	}

}
